package com.example.yoho.mvp.view.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerStrlloComponent;
import com.example.yoho.di.moudles.StrlloModules;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.StrlloContract;
import com.example.yoho.mvp.model.entity.StrlloListEntity;
import com.example.yoho.mvp.model.entity.StrlloSortEntity;
import com.example.yoho.mvp.presenter.StrlloPresenter;
import com.example.yoho.mvp.view.adapter.MyStrlloAdapter;
import com.example.yoho.utils.MyCustom;
import com.example.yoho.utils.MyGlide;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.view.CategoryHeadRefreshView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class StrlloFragment extends BaseFragment<StrlloPresenter> implements StrlloContract.View {

    private static final String TAG = "StrlloFragment";
    private com.flyco.tablayout.CommonTabLayout strlloComtab;
    private android.support.v7.widget.RecyclerView strlloRecyc;
    private List<CustomTabEntity> list_entity=new ArrayList<>();
    private MyStrlloAdapter adapter;
    private List<StrlloListEntity.ValuesBean> beanList=new ArrayList<>();
    private List<String> list_banner=new ArrayList<>();
    private Banner mybanner;
    private View view;
    private PullToRefreshLayout strlloPull;


    @Override
    public int bondLayout() {
        return R.layout.fragment_strllo;
    }

    @Override
    public void Inject() {
        DaggerStrlloComponent.builder().strlloModules(new StrlloModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {
        strlloComtab = (CommonTabLayout) findviewByIds(R.id.strllo_comtab);
        strlloRecyc = (RecyclerView) findviewByIds(R.id.strllo_recyc);
        strlloPull = (PullToRefreshLayout) findviewByIds(R.id.strllo_pull);
        strlloPull.setHeaderView(new CategoryHeadRefreshView(getContext()));
         view = LayoutInflater.from(getContext()).inflate(R.layout.strllo_headview, null);
         mybanner=view.findViewById(R.id.strllo_banners);



    }


    @Override
    public void initData() {

        mPresenter.reuqestStrllo();
        adapter=new MyStrlloAdapter(R.layout.strllo_item,beanList);
        strlloRecyc.setAdapter(adapter);
        strlloRecyc.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.addHeaderView(view);
        strlloPull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                beanList.clear();
                list_entity.clear();
                mPresenter.reuqestStrllo();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void loadMore() {
                mPresenter.reuqestStrllo();
            }
        });
    }

    @Override
    public void showStrlloSort(StrlloSortEntity strlloSortEntity) {
        List<StrlloSortEntity.ValuesBean> values = strlloSortEntity.getValues();
        Log.i(TAG, "showStrlloSort: "+values.size());
        for (int i = 0; i < values.size(); i++) {
            list_entity.add(new MyCustom(values.get(i).getSee_category_name(),0,0));
        }
        strlloComtab.setTabData((ArrayList<CustomTabEntity>) list_entity);

    }



    @Override
    public void showStrolloList(StrlloListEntity strlloListEntity) {
        List<StrlloListEntity.ValuesBean> values = strlloListEntity.getValues();
        beanList.addAll(values);
        adapter.notifyDataSetChanged();
        imageerloader(strlloListEntity);

        strlloPull.finishRefresh();
        strlloPull.finishLoadMore();
    }

    private void imageerloader(StrlloListEntity strlloListEntity) {
        List<StrlloListEntity.BannerBean> banner = strlloListEntity.getBanner();
        for (int i = 0; i < banner.size(); i++) {
            list_banner.add(Domain_Url.IMAGE_URL+banner.get(i).getRecommend_url());
        }
        mybanner.setImages(list_banner);
        mybanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                MyGlide.GlideAS(context,path,imageView);
            }
        });
        mybanner.start();
    }
}
